// /////////////////////////////////// 
//  
//  
//     (c)Copyright 2001-2010 Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 


using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace CCF20Model
{

    public class CustomerDetailData : INotifyPropertyChanged
    {
        private string _vehicleLicense = string.Empty;
        private string _name = string.Empty;
        public event PropertyChangedEventHandler PropertyChanged;

        [DisplayName("车辆型号")]
        public string VehicleType { get; set; }
        
        //[DisplayName("车牌号")]
        public string VehicleLicense 
        { 
            get
            {
                return this._vehicleLicense;
            }
            set
            {
                if (value != this._vehicleLicense)
                {
                    this._vehicleLicense = value;
                    NotifyPropertyChanged("VehicleLicense");
                }

            }
            
        }

        [DisplayName("底盘号")]
        public string ChassiNo { get; set; }
        [DisplayName("车辆销售日期")]
        public DateTime? VehicleSaleDate { get; set; }
        [DisplayName("排气量")]
        public string Displacement { get; set; }
        [DisplayName("发动机型号")]
        public string EngineType { get; set; }

        [DisplayName("销售价格")]
        public int? SalePrice { get; set; }
        [DisplayName("地区")]
        public string Location { get; set; }
        [DisplayName("厂牌")]
        public string Manufacture { get; set; }
        [DisplayName("年款")]
        public string ManufactureYear { get; set; }
        [DisplayName("销售的4S店")]
        public string ShopName { get; set; }


        public int CustomerID { get; set; }
        [DisplayName("客户号")]
        public string CustomerNo { get; set; }
        //[DisplayName("客户姓名")]
        public string Name
        {
            get
            {
                return this._name;
            }
            set
            {
                if (value != this._name)
                {
                    this._name = value;
                    NotifyPropertyChanged("Name");
                }

            }
        }
        [DisplayName("联系人")]
        public string ContactName { get; set; }
        [DisplayName("联系手机")]
        public string FirstMobile { get; set; }

        [DisplayName("身份证号码")]
        public string IdentityNo { get; set; }

        [DisplayName("是否转移客户")]
        public bool? IsTransfered { get; set; }

        [DisplayName("联系人职位")]
        public string ContactJob { get; set; }

        [DisplayName("联系人传真")]
        public string ContactFax { get; set; }

        [DisplayName("联系人电话2")]
        public string SecondMobile { get; set; }

        [DisplayName("联系人电话3")]
        public string ThirdMobile { get; set; }

        [DisplayName("性别")]
        public bool? Gender { get; set; }

        [DisplayName("备注")]
        public string Remark { get; set; }

        [DisplayName("创建日期")]
        public DateTime? CreateDate { get; set; }
        

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }

    public class CustomerData1 : INotifyPropertyChanged
    {
        private string _name = string.Empty;
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        public int CustomerID { get; set; }
        [DisplayName("客户号")]
        public string CustomerNo { get; set; }
        [DisplayName("客户姓名")]
        public string Name
        {
            get
            {
                return this._name;
            }
            set
            {
                if (value != this._name)
                {
                    this._name = value;
                    NotifyPropertyChanged("Name");
                }

            }
        }
        [DisplayName("联系人")]
        public string ContactName { get; set; }
        [DisplayName("联系手机")]
        public string FirstMobile { get; set; }

        [DisplayName("身份证号码")]
        public string IdentityNo { get; set; }

        [DisplayName("是否转移客户")]
        public bool? IsTransfered { get; set; }

        [DisplayName("联系人职位")]
        public string ContactJob { get; set; }

        [DisplayName("联系人传真")]
        public string ContactFax { get; set; }

        [DisplayName("联系人电话2")]
        public string SecondMobile { get; set; }

        [DisplayName("联系人电话3")]
        public string ThirdMobile { get; set; }

        [DisplayName("性别")]
        public bool? Gender { get; set; }

        [DisplayName("备注")]
        public string Remark { get; set; }

        [DisplayName("创建日期")]
        public DateTime? CreateDate { get; set; }
        
        
        
    
    }
    public class CustomerData 
    {
        private Customer _myCustomer;
        private Vehicle _myVehicle;
        public Customer MyCustomer
         {
             get
             {
                 return _myCustomer;
             }

             set
             {
                 if (value == null)
                 {
                     throw new Exception("Not Good");
                 }
                 else
                 {
                     _myCustomer = value;
                 }
             }
        }
        public Vehicle MyVehicle
        {
            get
            {
                return _myVehicle;
            }

            set
            {
                if (value == null)
                {
                    throw new Exception("Not Good");
                }
                else
                {
                    _myVehicle = value;
                }
            }
        }
        public Device MyDevice { get; set; }
        public TransferedCustomer MyTransfered { get; set; }
    }
    public class CustomerCollection : ObservableCollection<CustomerData>
    {
        private CCF20Entities ccf20Entities;
        private string GetLastCustomerNo()
        {
            Customer lastCustomer = ccf20Entities.Customers.OrderByDescending(c => c.CustomerID).FirstOrDefault();
            if (lastCustomer != null)
            {
                return lastCustomer.CustomerNo;
            }
            else
            {
                return null;
            }
        }
        
        protected override void InsertItem(int index, CustomerData item)
        {
            item.MyCustomer = new Customer();
            item.MyCustomer.CustomerNo = GetLastCustomerNo();
            item.MyVehicle = new Vehicle();
            item.MyTransfered = new TransferedCustomer();
            item.MyCustomer.Vehicles.Add(item.MyVehicle);
            item.MyCustomer.TransferedCustomers.Add(item.MyTransfered);
            base.InsertItem(index, item);
            //ccf20Entities.SaveChanges();
        }

        protected override void RemoveItem(int index)
        {

            Customer currentCustomer = ((CustomerData)this[index]).MyCustomer;
            if (currentCustomer.CustomerID > 0)
            {
                if (currentCustomer.Devices.FirstOrDefault() != null)
                {
                    // Remove Customer, if also remove the association with device
                    currentCustomer.Devices.Remove(currentCustomer.Devices.First());
                }
                ccf20Entities.DeleteObject(currentCustomer.TransferedCustomers.FirstOrDefault());
                ccf20Entities.DeleteObject(currentCustomer.Vehicles.FirstOrDefault());
                ccf20Entities.DeleteObject(currentCustomer);
                //ccf20Entities.SaveChanges();
            }
            base.RemoveItem(index);
            
        }
        public CustomerCollection(IEnumerable<CustomerData> customers, CCF20Entities context):base(customers)
        {
            ccf20Entities = context;
        }
        
    }
    public class SimpleCustomerInfo
    {
        public string Name { get; set; }
        public int ID { get; set; }
        public string VehicleModel { get; set; }
        public string VehicleLicense { get; set; }
    }
    public class CustomerModel : BaseModel
    {
        public CustomerCollection MyCustomerData;
        public IEnumerable<VehicleType> MyVehicleTypes;
        public IEnumerable<SimpleCustomerInfo> MySimpleCustomerInfo;
        public void GetSimpleCustomerInfo()
        {
            MySimpleCustomerInfo = from c in ccf20Entities.Customers
                                       select new SimpleCustomerInfo
                                       {
                                           Name = c.Name,
                                           ID = c.CustomerID,
                                           VehicleModel = c.Vehicles.FirstOrDefault().VehicleType.Model,
                                           VehicleLicense = c.Vehicles.FirstOrDefault().VehicleLicense
                                       };
        }
        public void GetMyVehicleTypes()
        {
            if (ZICMonitorModel.IsJava)
            {
                MyVehicleTypes = from v in ccf20Entities.VehicleTypes
                                 select v;
            }
            else
            {
                MyVehicleTypes = from v in ccf20Entities.VehicleTypes
                                 where v.Manufacture.Equals("FAW-VW")
                                 select v;
            }

        }
        public CustomerCollection GetCustomer()
        {
            var query = from c in ccf20Entities.Customers
                        select new CustomerData
                        {
                            MyCustomer = c,
                            MyVehicle = c.Vehicles.FirstOrDefault(),
                            MyDevice = c.Devices.FirstOrDefault(),
                            MyTransfered = c.TransferedCustomers.FirstOrDefault()
                        };
            return new CustomerCollection(query, ccf20Entities);
            
        }

        public void Save(CustomerData cd)
        {
            if ((cd != null) && (cd.MyCustomer.CustomerID == 0))
            {
                ccf20Entities.AddToCustomers(cd.MyCustomer);
                ccf20Entities.AddToVehicles(cd.MyVehicle);
                ccf20Entities.AddToTransferedCustomers(cd.MyTransfered);
            }
            ccf20Entities.SaveChanges();
        
        }
        public void ConfirmedTransferCustomer(CustomerData cd,string deviceType, string terminalType,DateTime registerDate)
        {
            Device device = new Device();
            ccf20Entities.Devices.AddObject(device);
            device.IDCSimNumber = cd.MyTransfered.IDCNum;
            device.DeviceKey = cd.MyTransfered.DeviceKey;
            device.VehicleID = cd.MyVehicle.VehicleID;
            device.CustomerID = cd.MyCustomer.CustomerID;
            device.DeviceStatus = "售出";
            device.IsRegistered = true;
            device.IsManual = true;
            device.DeviceType = deviceType;
            device.TerminalType = terminalType;
            device.RegisterDate = registerDate;
            ccf20Entities.SaveChanges();
        }
       
        
    }
}
